home *** CD-ROM | disk | FTP | other *** search
- // Saved by ViRC 2.0 at 6/15/2001 4:50:01 PM
-
- // Whiteboard tic-tac-toe game for Visual IRC 2.0pre8 and above
- // Jesse McGrew
-
- if ($build < 200) || (($prebuild != 0) && ($prebuild < 8))
- MessageBox This script requires ViRC 2.0pre8 or later.
- halt
- endif
-
- -@ $TTT.*
-
- // Toolbars
-
- Toolbar TB_TICTACTOE "Tic-tac-toe" for whiteboards docked top position last
- T_PLAY 0 TTT | Play tic-tac-toe
- T_REDRAW 0 Redraw | Redraw tic-tac-toe board
- EndToolbar
-
- ToolbarItem T_PLAY on TB_TICTACTOE
- //@l $id = $getwindowid($C)
- @l $id = $strtokr(! $C)
- if [${TTT.$id.State}] != []
- @l $ret = $messagedlg(33 This will end the game in progress.)
- halt if $ret != 1
- endif
- @ $TTT.$id.State = 0
- @ $TTT.$id.Grid = ---------
- TTT_RedrawBoard $id
- EndToolbarItem
-
- ToolbarItem T_REDRAW on TB_TICTACTOE
- @l $id = $strtokr(! $C)
- TTT_RedrawBoard $id
- EndToolbarItem
-
-
- Event <OnMyWhiteboardAction_TTT> "% DR *"
- // local user is X (moves in state 0)
-
- @l $id = $strtokr(! $0)
- @l $grid = ${TTT.$id.Grid}
- if ([$grid] != []) && (${TTT.$id.State} == 0)
- // extract x and y from command
- @l $cmd = $listsplit(, $2-)
- @l $x = $listindex(4 $cmd)
- @l $y = $listindex(5 $cmd)
- // see if it's in a space
- for (@l $i = 1; $i <= 9; $i++)
- @l $x1 = $_ttt($i x1)
- @l $y1 = $_ttt($i y1)
- @l $x2 = $_ttt($i x2)
- @l $y2 = $_ttt($i y2)
- if ($x >= $x1) && ($x <= $x2) && ($y >= $y1) && ($y <= $y2)
- // got a space.. check the value
- @l $value = $substr($grid $i 1)
- if [$value] == [-]
- // the space is empty
- @ $TTT.$id.Grid = $substr($grid 1 $($i - 1))X$substr($grid $($i + 1) 9)
- @ $TTT.$id.State = 1
- TTT_RedrawBoard $id
- TTT_CheckWinner $id
- endif
- halt
- endif
- endfor
- endif
- EndEvent
-
- Event <OnWhiteboardAction_TTT> "% DR *"
- // remote user is O (moves in state 1)
-
- @l $id = $strtokr(! $0)
- @l $grid = ${TTT.$id.Grid}
- if ([$grid] != []) && (${TTT.$id.State} == 1)
- // extract x and y from command
- @l $cmd = $listsplit(, $2-)
- @l $x = $listindex(4 $cmd)
- @l $y = $listindex(5 $cmd)
- // see if it's in a space
- for (@l $i = 1; $i <= 9; $i++)
- @l $x1 = $_ttt($i x1)
- @l $y1 = $_ttt($i y1)
- @l $x2 = $_ttt($i x2)
- @l $y2 = $_ttt($i y2)
- if ($x >= $x1) && ($x <= $x2) && ($y >= $y1) && ($y <= $y2)
- // got a space.. check the value
- @l $value = $substr($grid $i 1)
- if [$value] == [-]
- // the space is empty
- @ $TTT.$id.Grid = $substr($grid 1 $($i - 1))O$substr($grid $($i + 1) 9)
- @ $TTT.$id.State = 0
- TTT_RedrawBoard $id
- TTT_CheckWinner $id
- endif
- halt
- endif
- endfor
- endif
- EndEvent
-
-
- Alias TTT_REDRAWBOARD
- halt if [${TTT.$1.State}] == []
-
- @l $win = =!$1
- // clear canvas
- Whiteboard $win cls
- // draw title
- switch $TTT.$1.State
- case 0:
- TTT_Title $1 X's turn
- case 1:
- TTT_Title $1 O's turn
- case else
- TTT_Title $1 ViRC Tic-Tac-Toe
- endswitch
- // draw grid contents
- @l $grid = ${TTT.$1.Grid}
- halt if [$grid] == []
- if [$grid] != []
- for (@l $i = 1; $i <= 9; $i++)
- @l $x1 = $_ttt($i x1)
- @l $y1 = $_ttt($i y1)
- @l $x2 = $_ttt($i x2)
- @l $y2 = $_ttt($i y2)
- switch $substr($grid $i 1)
- case X:
- // fill blue
- Whiteboard $win dr 5 2 clBlue clBlue $x1,$y1 $x2,$y2
- // NW-SE
- Whiteboard $win dr 1 2 clBlack clBlack $($x1 + 5),$($y1 + 5) $($x2 - 5),$($y2 - 5)
- // NE-SW
- Whiteboard $win dr 1 2 clBlack clBlack $($x2 - 5),$($y1 + 5) $($x1 + 5),$($y2 - 5)
- case O:
- // fill red
- Whiteboard $win dr 5 2 clRed clRed $x1,$y1 $x2,$y2
- // circle
- Whiteboard $win dr 4 2 clBlack clBlack $($x1 + 5),$($y1 + 5) $($x2 - 5),$($y2 - 5)
- endswitch
- endfor
- endif
-
- // draw grid lines
- // vertical left: (2.x1,2.y1)-(7.x2,7.y2)
- Whiteboard $win dr 1 2 clBlack clBlack $_ttt(2 x1),$_ttt(2 y1) $_ttt(7 x2),$_ttt(7 y2)
- // vertical right: (3.x1,3.y1)-(8.x2,8.y2)
- Whiteboard $win dr 1 2 clBlack clBlack $_ttt(3 x1),$_ttt(3 y1) $_ttt(8 x2),$_ttt(8 y2)
- // horizontal top: (4.x1,4.y1)-(6.x2,6.y1)
- Whiteboard $win dr 1 2 clBlack clBlack $_ttt(4 x1),$_ttt(4 y1) $_ttt(6 x2),$_ttt(6 y1)
- // horizontal bottom: (7.x1,7.y1)-(9.x2,9.y1)
- Whiteboard $win dr 1 2 clBlack clBlack $_ttt(7 x1),$_ttt(7 y1) $_ttt(9 x2),$_ttt(9 y1)
- EndAlias
-
- Alias _TTT
- @l $GRID_BASEX = 10
- @l $GRID_BASEY = 70
- @l $GRID_SIZE = 50
-
- @l $row = $(($1 - 1) / 3)
- @l $col = $(($1 - 1) % 3)
- @l $x1 = $($col * $GRID_SIZE + $GRID_BASEX)
- @l $y1 = $($row * $GRID_SIZE + $GRID_BASEY)
-
- switch $2
- case x1:
- @ $fresult = $x1
- case y1:
- @ $fresult = $y1
- case x2:
- @ $fresult = $($x1 + $GRID_SIZE)
- case y2:
- @ $fresult = $($y1 + $GRID_SIZE)
- case xc:
- @ $fresult = $($x1 + $GRID_SIZE / 2)
- case yc:
- @ $fresult = $($y1 + $GRID_SIZE / 2)
- endswitch
- EndAlias
-
- Alias TTT_CHECKWINNER
- @l $id = $1
- // horizontal
- foreach ($a,$b,$c; 1 2 3 4 5 6 7 8 9)
- if $ttt_line($id $a $b $c)
- TTT_Winner $id $a $b $c
- halt
- endif
- endforeach
-
- // vertical
- foreach ($a,$b,$c; 1 4 7 2 5 8 3 6 9)
- if $ttt_line($id $a $b $c)
- TTT_Winner $id $a $b $c
- halt
- endif
- endforeach
-
- // diagonal
- if $ttt_line($id 1 5 9)
- TTT_Winner $id 1 5 9
- halt
- endif
- if $ttt_line($id 3 5 7)
- TTT_Winner $id 3 5 7
- halt
- endif
-
- // cat's game?
- if $strpos(- $TTT.$id.Grid) == 0
- TTT_Title $id Cat's game :(
- -@ $TTT.$id.*
- halt
- endif
- EndAlias
-
- Alias TTT_WINNER
- @l $id = $1
- // starting and ending space
- @l $s1 = $2
- @l $s2 = $4
- // draw a line
- Whiteboard =!$id dr 1 24 clYellow clYellow $_ttt($s1 xc),$_ttt($s1 yc) $_ttt($s2 xc),$_ttt($s2 yc)
- // draw winner's title
- TTT_Title $id $substr($TTT.$id.Grid $s1 1) wins!!
- // end game
- -@ $TTT.$id.*
- EndAlias
-
- Alias TTT_TITLE
- // draw box for title
- Whiteboard =!$1 dr 5 2 clBlack clYellow 10,10 190,50
- // draw title
- Whiteboard =!$1 txtex 20,20 Arial,14 2 clBlack clYellow $2-
- EndAlias
-
- Alias TTT_LINE
- @l $grid = ${TTT.$1.Grid}
- if [$grid] == []
- @ $fresult = 0
- halt
- endif
-
- // get space values
- @l $v1 = $substr($grid $2 1)
- @l $v2 = $substr($grid $3 1)
- @l $v3 = $substr($grid $4 1)
- // return 1 if s1 isn't empty, s1 == s2, and s1 == s3
- @ $fresult = $(([$v1] != [-]) && ([$v1] == [$v2]) && ([$v1] == [$v3]))
- EndAlias
-